package com.awenhui.demo.module.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 自定义查询用户逻辑
 * @author yuxf
 * @version 1.0
 * @date 2020/10/20 10:23
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    Logger logger = LoggerFactory.getLogger(getClass());


    public  static HashMap<String,String> users=new HashMap<>();
    static {
        users.put("admin",new BCryptPasswordEncoder().encode("123456a"));
        users.put("yuxf",new BCryptPasswordEncoder().encode("123456a"));
        users.put("demo",new BCryptPasswordEncoder().encode("123456a"));
    }
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        String pwd = users.get(s);
       logger.info("用户密码是："+pwd);
        return new User(s,pwd, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
